#ifndef __U8G2_HAL_H
#define __U8G2_HAL_H

#include "at32f435_437.h"
#include "at32f435_437_board.h"
#include "i2c_application.h"

#include "u8g2.h"

#include "FreeRTOS.h"
#include "task.h"

#include "rtos_bsp.h"

#define I2C_TIMEOUT                      0xFFFFFFF
#define I2CA_CLKCTRL                     0x10d01626

#define I2CA_ADDRESS                     0xA0

#define I2CA_PORT                        I2C1
#define I2CA_CLK                         CRM_I2C1_PERIPH_CLOCK
#define I2CA_DMA                         DMA1
#define I2CA_DMA_CLK                     CRM_DMA1_PERIPH_CLOCK

#define I2CA_SCL_GPIO_CLK                CRM_GPIOB_PERIPH_CLOCK
#define I2CA_SCL_GPIO_PIN                GPIO_PINS_6
#define I2CA_SCL_GPIO_PinsSource         GPIO_PINS_SOURCE6
#define I2CA_SCL_GPIO_PORT               GPIOB
#define I2CA_SCL_GPIO_MUX                GPIO_MUX_4

#define I2CA_SDA_GPIO_CLK                CRM_GPIOB_PERIPH_CLOCK
#define I2CA_SDA_GPIO_PIN                GPIO_PINS_7
#define I2CA_SDA_GPIO_PinsSource         GPIO_PINS_SOURCE7
#define I2CA_SDA_GPIO_PORT               GPIOB
#define I2CA_SDA_GPIO_MUX                GPIO_MUX_4

#define I2CA_DMA_TX_Channel              DMA1_CHANNEL3
#define I2CA_DMA_TX_DMAMUX_Channel       DMA1MUX_CHANNEL3
#define I2CA_DMA_TX_DMAREQ               DMAMUX_DMAREQ_ID_I2C1_TX
#define I2CA_DMA_TX_IRQn                 DMA1_Channel3_IRQn

#define I2CA_DMA_RX_Channel              DMA1_CHANNEL4
#define I2CA_DMA_RX_DMAMUX_Channel       DMA1MUX_CHANNEL4
#define I2CA_DMA_RX_DMAREQ               DMAMUX_DMAREQ_ID_I2C1_RX
#define I2CA_DMA_RX_IRQn                 DMA1_Channel4_IRQn

#define I2CA_EVT_IRQn                    I2C1_EVT_IRQn
#define I2CA_ERR_IRQn                    I2C1_ERR_IRQn
#define I2CA_ERR_IRQn                    I2C1_ERR_IRQn

// I2CB
#define I2CB_CLKCTRL                     0x30f0192c

#define I2CB_ADDRESS                     0xA0

#define I2CB_PORT                        I2C3
#define I2CB_CLK                         CRM_I2C3_PERIPH_CLOCK
#define I2CB_DMA                         DMA2
#define I2CB_DMA_CLK                     CRM_DMA2_PERIPH_CLOCK

#define I2CB_SCL_GPIO_CLK                CRM_GPIOC_PERIPH_CLOCK
#define I2CB_SCL_GPIO_PIN                GPIO_PINS_0
#define I2CB_SCL_GPIO_PinsSource         GPIO_PINS_SOURCE0
#define I2CB_SCL_GPIO_PORT               GPIOC
#define I2CB_SCL_GPIO_MUX                GPIO_MUX_4

#define I2CB_SDA_GPIO_CLK                CRM_GPIOC_PERIPH_CLOCK
#define I2CB_SDA_GPIO_PIN                GPIO_PINS_1
#define I2CB_SDA_GPIO_PinsSource         GPIO_PINS_SOURCE1
#define I2CB_SDA_GPIO_PORT               GPIOC
#define I2CB_SDA_GPIO_MUX                GPIO_MUX_4

#define I2CB_DMA_TX_Channel              DMA2_CHANNEL1
#define I2CB_DMA_TX_DMAMUX_Channel       DMA2MUX_CHANNEL1
#define I2CB_DMA_TX_DMAREQ               DMAMUX_DMAREQ_ID_I2C3_TX
#define I2CB_DMA_TX_IRQn                 DMA2_Channel1_IRQn

#define I2CB_DMA_RX_Channel              DMA2_CHANNEL2
#define I2CB_DMA_RX_DMAMUX_Channel       DMA2MUX_CHANNEL2
#define I2CB_DMA_RX_DMAREQ               DMAMUX_DMAREQ_ID_I2C3_RX
#define I2CB_DMA_RX_IRQn                 DMA2_Channel2_IRQn

#define I2CB_EVT_IRQn                    I2C3_EVT_IRQn
#define I2CB_ERR_IRQn                    I2C3_ERR_IRQn

void u8g2_i2c_init(void);
void eeprom_i2c_init(void);
uint8_t u8x8_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
uint8_t u8x8_byte_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);

#endif